import { ref } from "vue";
import { ElMessageBox, ElMessage } from "element-plus";
export default function useUpdateStatus() {
  const idList = ref([]); //批量
  async function updateStatusHandler({
    status,
    api,
    hideMessageBox = true,
    sendData,
    multiple = false
  }) {
    return new Promise(async (resolve, reject) => {
      try {
        if (multiple && !idList.value?.length) {
          ElMessage({
            type: "warning",
            message: "请至少选择一项！"
          });
          reject(false);
          return;
        }
        const message = multiple
          ? `确认更新已选中的数据项状态?`
          : `确认${status == 1 ? "停用" : "启用"}已选中的数据项?`;
        if (!hideMessageBox) {
          await ElMessageBox.confirm(message, "警告", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          });
        }
        await api(sendData);
        ElMessage({
          type: "success",
          message: "操作成功"
        });
        idList.value = [];
        resolve(true);
      } catch (error) {
        reject(error);
      }
    });
  }
  //多选
  function select(data) {
    console.info(data);
    idList.value = data.idList;
  }
  return {
    updateStatusHandler,
    select,
    idList
  };
}
